<?php
namespace common\vendor;
use linslin\yii2\curl\Curl;

/**
 * 腾讯云
 * Created by PhpStorm.
 * User: eycuit
 * Date: 2020/2/27
 * Time: 15:14
 */

class AuthApi {

    public $secretId = 'AKID9cu14Pc8jcvR4oQPNa4epir7yah3HdsqjNna';
    public $secretKey = '63i3mq5Z815JDnxB5H68zi4sivI517olHTxZP82';
    public $source = 'market';

    private function getAuth($datetime = '') {
        // 签名
//        $datetime = gmdate('D, d M Y H:i:s T');
        $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $this->source);
        $sign = base64_encode(hash_hmac('sha1', $signStr, $this->secretKey, true));
        return sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $this->secretId, $sign);
    }


    /**
     * 身份证二要素认证
     * 参考地址 https://market.aliyun.com/products/57000002/cmapi00037883.html?spm=5176.11065268.1996646101.searchclickresult.24235676EZIjty#sku=yuncode3188300002
     * @param string $name
     * @param string $idNumber
     *
     * @return mixed
     */
    public function authIdentity($name = '', $idNumber = '') {

        $apiUrl = 'http://service-m6b92bj7-1300755093.bj.apigw.tencentcs.com/release/idcard/VerifyIdcardv2';

        $datetime = gmdate('D, d M Y H:i:s T');

        $auth = static::getAuth($datetime);

        $curl = new Curl();

        $curl->setHeader('X-Source', $this->source);
        $curl->setHeader('X-Date', $datetime);
        $curl->setHeader('Authorization', $auth);

//        $params['realName'] = urlencode($name);
        $params['realName'] = $name;
        $params['cardNo'] = $idNumber;
        $curl->setGetParams($params);
        $res = $curl->get($apiUrl);
        return json_decode($res, true);
    }



    /**
     * 银行卡三要素认证
     * 参考地址 https://market.aliyun.com/products/57000002/cmapi033466.html?spm=5176.730005.productlist.d_cmapi033466.369d3524yVdNMt&innerSource=search_%E9%93%B6%E8%A1%8C%E5%8D%A1%E4%B8%89%E8%A6%81%E7%B4%A0#sku=yuncode2746600001
     * @param string $name
     * @param string $cardNo
     * @param string $idNumber
     *
     * @return mixed
     */
    public function authBank($name = '', $idNumber = '', $cardNo = '') {

        $apiUrl = 'https://service-fwuhszm1-1256140209.ap-shanghai.apigateway.myqcloud.com/release/bankcard3_c/get';

        $datetime = gmdate('D, d M Y H:i:s T');

        $auth = static::getAuth($datetime);

        $curl = new Curl();

        $curl->setHeader('X-Source', $this->source);
        $curl->setHeader('X-Date', $datetime);
        $curl->setHeader('Authorization', $auth);

        $params['bankcard'] = $cardNo;
        $params['name'] = $name;
        $params['idcard'] = $idNumber;

        $curl->setGetParams($params);
        $res = $curl->get($apiUrl);
        return json_decode($res, true);
    }

    public static function queryBankInfo($cardNo = '') {
        //https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=银行卡卡号&cardBinCheck=true

        $url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo={$cardNo}&cardBinCheck=true";

        $curl = new Curl();

//        $params['_input_charset'] = 'utf-8';
//        $params['cardNo'] = $cardNo;
//        $params['cardBinCheck'] = true;
//        $curl->setGetParams($params);

        $res = $curl->get($url);
        return json_decode($res, true);

    }

    public function send() {

    }

}